package infolayer2.filter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import infolayer2.graph.Graph;
import infolayer2.graph.Node;

public class TemplateFileImport {
  
  
  private Graph graph;

  public TemplateFileImport(Graph graph) {
    this.graph = graph;
  }
  
  public void process(File dir) throws IOException {
    for (File f: dir.listFiles()) {
      String name = f.getName();
      if (!name.endsWith(".html")) {
        continue;
      }
      name = name.substring(0, name.length() - 5);
      Node node = graph.getOrCreate("/template/" + name);
      BufferedReader reader = new BufferedReader(new FileReader(f));
      StringBuilder sb = new StringBuilder();
      while(true) {
        String line = reader.readLine();
        if (line == null) {
          break;
        }
        sb.append(line);
        sb.append('\n');
      }
      node.setReference("/type/object/type", "/template");
      node.setValue("/type/object/name", name);
      node.setValue("/template/content", sb.toString());
    }
  }
}